package com.njwb;
import com.mysql.cj.xdevapi.Table;
import com.njwb.mapper.StudentMapper;
import com.njwb.pojo.Student;
import com.njwb.util.MybatisUtil;
import org.apache.ibatis.session.SqlSession;

import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        boolean flag = true;
        while (flag) {
            System.out.println("\n===== 学生管理系统 =====");
            System.out.println("1：完成学生表的 ADD 操作");
            System.out.println("2：根据 SID 修改某个学生的信息");
            System.out.println("3：根据 SID 删除某个学生信息");
            System.out.println("4：根据 SID 查询单个学生信息");
            System.out.println("5：查询所有学生信息");
            System.out.println("6：根据地址模糊查询学生信息");
            System.out.println("7：模糊查询 + 分页显示");
            System.out.println("0：退出系统");
            System.out.println("请输入操作：");

            int x = scanner.nextInt();
            scanner.nextLine(); // 吃掉换行符，避免 nextLine 出问题

            switch (x) {
                case 1:
                    SqlSession sqlSession = MybatisUtil.getSqlSession();
                    try {
                        StudentMapper mapper = sqlSession.getMapper(StudentMapper.class);

                        System.out.print("请输入学生姓名：");
                        String name = scanner.nextLine();

                        System.out.print("请输入学生年龄：");
                        int age = scanner.nextInt();
                        scanner.nextLine(); // 清理换行符

                        System.out.print("请输入学生爱好：");
                        String like = scanner.nextLine();

                        System.out.print("请输入学生地址：");
                        String addr = scanner.nextLine();

                        mapper.addStudent(new Student(name, age, like, addr));

                        sqlSession.commit();
                        System.out.println(">>> 学生添加成功！");
                    } catch (Exception e) {
                        sqlSession.rollback();
                        e.printStackTrace();
                    } finally {
                        MybatisUtil.closeSqlsession();
                    }
                    break;



                case 2:
                    sqlSession = MybatisUtil.getSqlSession();
                    try {
                        StudentMapper mapper = sqlSession.getMapper(StudentMapper.class);

                        System.out.print("请输入要修改的学生ID：");
                        int id = scanner.nextInt();
                        scanner.nextLine();

                        System.out.print("请输入新姓名：");
                        String name2 = scanner.nextLine();
                        System.out.print("请输入新年龄：");
                        int age2 = scanner.nextInt();
                        scanner.nextLine();
                        System.out.print("请输入新爱好：");
                        String like2 = scanner.nextLine();
                        System.out.print("请输入新地址：");
                        String addr2 = scanner.nextLine();

                        Student stu = new Student(name2, age2, like2, addr2);
                        stu.setSid(id);

                        mapper.updateStudentById(stu);
                        sqlSession.commit();
                        System.out.println(">>> 修改成功！");
                    } catch (Exception e) {
                        sqlSession.rollback();
                        e.printStackTrace();
                    } finally {
                        MybatisUtil.closeSqlsession();
                    }
                    break;

                case 3:
                    sqlSession = MybatisUtil.getSqlSession();
                    try {
                        StudentMapper mapper = sqlSession.getMapper(StudentMapper.class);

                        System.out.print("请输入要删除的学生ID：");
                        int idDel = scanner.nextInt();

                        mapper.deleteStudentById(idDel);
                        sqlSession.commit();
                        System.out.println(">>> 删除成功！");
                    } catch (Exception e) {
                        sqlSession.rollback();
                        e.printStackTrace();
                    } finally {
                        MybatisUtil.closeSqlsession();
                    }
                    break;

                case 4:
                    sqlSession = MybatisUtil.getSqlSession();
                    try {
                        StudentMapper mapper = sqlSession.getMapper(StudentMapper.class);

                        System.out.print("请输入要查询的学生ID：");
                        int idSel = scanner.nextInt();

                        Student stu = mapper.queryStudentById(idSel);
                        System.out.println(stu != null ? stu : "未找到学生！");
                    } finally {
                        MybatisUtil.closeSqlsession();
                    }
                    break;

                case 5:
                    sqlSession = MybatisUtil.getSqlSession();
                    try {
                        StudentMapper mapper = sqlSession.getMapper(StudentMapper.class);
                        for (Student stu : mapper.queryAllStudents()) {
                            System.out.println(stu);
                        }
                    } finally {
                        MybatisUtil.closeSqlsession();
                    }
                    break;

                case 6:
                    sqlSession = MybatisUtil.getSqlSession();
                    try {
                        StudentMapper mapper = sqlSession.getMapper(StudentMapper.class);

                        System.out.print("请输入要模糊查询的地址关键字：");
                        String addrSel = scanner.nextLine();

                        for (Student stu : mapper.queryStudentByAddress(addrSel)) {
                            System.out.println(stu);
                        }
                    } finally {
                        MybatisUtil.closeSqlsession();
                    }
                    break;

                case 7:
                    sqlSession = MybatisUtil.getSqlSession();
                    try {
                        StudentMapper mapper = sqlSession.getMapper(StudentMapper.class);

                        System.out.print("请输入姓名关键字：");
                        String nameLike = scanner.nextLine();
                        System.out.print("请输入每页大小：");
                        int pageSize = scanner.nextInt();
                        System.out.print("请输入第几页：");
                        int pageNum = scanner.nextInt();

                        int offset = (pageNum - 1) * pageSize;
                        for (Student stu : mapper.queryStudentsByPage(nameLike, offset, pageSize)) {
                            System.out.println(stu);
                        }
                    } finally {
                        MybatisUtil.closeSqlsession();
                    }
                    break;

                case 0:
                    System.out.println(">>> 系统退出，感谢使用！");
                    flag = false;
                    break;

                default:
                    System.out.println(">>> 输入无效，请重新输入！");
            }
        }
        scanner.close();
    }

}
